package cn.com.nes.site.action.iot;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.module.service.iot.DtuService;
import com.alibaba.fastjson2.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

@Controller("dtu")
@RequestMapping("/dtu")
public class DtuAction {

    @Resource
    private DtuService dtuService;

    /**
     * 模块管理-获取dtu列表
     */
    @RequestMapping("/getDtuList")
    @ResponseBody
    public PageData getDtuList(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.getDtuList(req);
        return res;
    }

    /**
     * 模块管理-绑定协议
     */
    @RequestMapping("/bindProtocol")
    @ResponseBody
    public PageData bindProtocol(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.updateDtuProtocol(req, false);
        return res;
    }

    /**
     * 模块管理-解绑协议
     */
    @RequestMapping("/unbindProtocol")
    @ResponseBody
    public PageData unbindProtocol(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.updateDtuProtocol(req, true);
        return res;
    }

    /**
     * 模块管理-获取实时数据
     */
    @RequestMapping("/getRealTimeData")
    @ResponseBody
    public PageData getRealTimeData(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.getRealTimeData(req);
        return res;
    }

    /**
     * 模块管理-获取历史数据
     */
    @RequestMapping("/getHistoryData")
    @ResponseBody
    public PageData getHistoryData(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.getHistoryData(req);
        return res;
    }

    /**
     * 获取续费套餐
     */
    @RequestMapping("/getRenewalPackage")
    @ResponseBody
    public PageData getRenewalPackage(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.getRenewalPackage(req);
        return res;
    }


    /**
     * 生成支付链接
     */
    @RequestMapping("/createPaymentLink")
    @ResponseBody
    public PageData createPaymentLink(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.createPaymentLink(req);
        return res;
    }

    /**
     * 支付回调
     */
    @RequestMapping("/wechatPayNotify")
    @ResponseBody
    public String wechatPayNotify(HttpServletRequest request, HttpServletResponse response) {
        PageData res = dtuService.wechatPayNotify(request);
        return JSON.toJSONString(res);
    }

    /**
     * 模块管理-查询支付状态
     */
    @RequestMapping("/queryPaymentStatus")
    @ResponseBody
    public PageData queryPaymentStatus(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.queryPaymentStatus(req);
        return res;
    }

    /**
     * 主动发起支付状态查询
     */
    @RequestMapping("/queryPaymentStatusBySelf")
    @ResponseBody
    public PageData queryPaymentStatusBySelf(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.queryPaymentStatusBySelf(req);
        return res;
    }

    /**
     * 查询流量信息
     */
    @RequestMapping("/getCardInfo")
    @ResponseBody
    public PageData getCardInfo(HttpServletRequest request, HttpServletResponse response) {
        DataRes req_Status = DBUtils.setRemoteVisit(request, response, true);
        PageData res;
        if (req_Status.getRetCode() != 0) {
            res = new PageData();
            res.setRetCode(req_Status.getRetCode());
            res.setRetMessage(req_Status.getRetMessage());
            return res;
        }
        DataReq req = req_Status.getReq();

        res = dtuService.getCardInfo(req);
        return res;
    }

}